package com.propic.www;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

import com.leapmotion.leap.Controller;
import com.leapmotion.leap.Listener;
import javafx.application.Application;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.StackPane;
import javafx.scene.shape.Shape;
import javafx.scene.text.Text;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class Mainwindows extends Application {

	public static String pathtmp = null;
    public static int count = 0;
    public static int count2 = 0;

	//总布局
	public static final BorderPane BorderMainpane = new BorderPane();
	public static final SplitPane Mainpane = new SplitPane();


	//上侧菜单栏
	public static final MenuBar mainMenuBar = new MenuBar();
	public static final Menu menuFile = new Menu("文件(F)");
	public static final Menu menuHelp = new Menu("帮助(H)");
	
	//①菜单小项（文件(F)）
	public static MenuItem openFile = new MenuItem("打开文件");
	public static final MenuItem exitAll = new MenuItem("退出");

	//④菜单小项（帮助(H)）
	public static final MenuItem helpMenu = new MenuItem("帮助");
	public static final MenuItem aboutPropic = new MenuItem("关于");


	//中间位置设置为StackPane
	public static final StackPane centerpane = new StackPane();
	//标志，如果centerpane内容为空，值为true，否则为false
	public static Boolean flag = true;
	
	//右键菜单①（主显示框的右键菜单）
	public static final ContextMenu cmRight = new ContextMenu();
	public static final MenuItem copyImage = new MenuItem("复制图片");
	public static final MenuItem openImage = new MenuItem("打开图片");
	public static final MenuItem Img_attriBute = new MenuItem("图片属性");
	public static final MenuItem renameImage = new MenuItem(" 重命名 ");
	public static final MenuItem Img_delete = new MenuItem("删除图片");
	//右键菜单②（目录树的右键菜单）
	public static final ContextMenu menuTree = new ContextMenu();
	public static final MenuItem cpAll = new MenuItem("粘贴全部文件到此目录");
	public static final MenuItem opAll = new MenuItem("显示该目录下的全部图片");
	public static final MenuItem showAll = new MenuItem("幻灯片播放该目录下的全部图片");
	public static final MenuItem showSigle = new MenuItem("图片浏览");
	
	public static final ImageView pic = new ImageView();
	
	//需要复制图片的List<Path>，或者是选中图像
	public static List<String> copyfile = new ArrayList<String>();
	
	//目录下全部图片文件List<pic>
	public static List<File> files = new ArrayList<File>();

	//搜索框
	public static final TextField textField = new TextField();
	public static final Button go = new Button();
	
	@Override
	public void start(Stage Mainstage) throws Exception {
		
		
		
		// TODO Auto-generated method stub
		//,禁止软件窗口放大缩小
		Mainstage.setResizable(false);
		Mainstage.setTitle("ProPic图像管理");

	
		menuFile.getItems().addAll(openFile,  exitAll);
		//设置动作
		//①打开一个文件
		openFile.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent arg0) {
				FileChooser fileChooser = new FileChooser();
	            fileChooser.getExtensionFilters().addAll(
	            		new FileChooser.ExtensionFilter("All Images", "*.*"),
	                    new FileChooser.ExtensionFilter("JPG", "*.jpg"),
	                    new FileChooser.ExtensionFilter("PNG", "*.png"),
	            		new FileChooser.ExtensionFilter("JPEG", "*.jpeg"),
	            		new FileChooser.ExtensionFilter("GIF", "*.gif"),
	            		new FileChooser.ExtensionFilter("BMP", "*.bmp")
	            );
				File file = fileChooser.showOpenDialog(Mainstage);
				if (file != null) {
					Image image = new Image("file:"+file.getAbsolutePath());
//					Scene showscence = OpensinglePhoto.showPhoto(image);
					Stage showstage = new Stage();
					showstage.setScene(OpensinglePhoto.showPhoto(image, showstage));
					showstage.initStyle(StageStyle.TRANSPARENT);
					showstage.isFocused();
					showstage.show();


				}
			
			}
		});
		//②关闭程序
		exitAll.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent arg0) {
				System.exit(0);		
		    }  
		});
		

		menuHelp.getItems().addAll(helpMenu, aboutPropic);
		helpMenu.setOnAction(e->{
			Stage helpstage = new Openbrowse().openbrowser("");
            helpstage.show();
		});
		aboutPropic.setOnAction(e->{
			new Helpstage().startHelp();
		});

		//全部集成，设置到顶部
		mainMenuBar.getMenus().addAll(menuFile, menuHelp);

		FlowPane flowPane = new FlowPane();
		Text texttmp = new Text("  ");
		flowPane.getChildren().addAll(textField,texttmp, go);
		StackPane stackpanetest = new StackPane();
		stackpanetest.getChildren().add(flowPane);
		stackpanetest.setMaxWidth(400);
		Image findimg = new Image(String.valueOf(getClass().getResource("res/spotlight.png")));
		ImageView findwhat = new ImageView(findimg);
		go.setGraphic(findwhat);

		//绑定搜索按钮的动作
		go.setOnAction(chlick->{
			String get = textField.getText();
			System.out.println("显示搜索的东西:"+get);
			if (get.equals("help")){
				Stage helpstage = new Openbrowse().openbrowser("");
				helpstage.show();
			}
			if (get.equals("about")){
				new Helpstage().startHelp();
			}
			if (get.equals("exit")){
				System.exit(0);
			}
			if (get.equals("技能")){
//				ImageCropWithRubberBand mm = new ImageCropWithRubberBand();
//				mm.launch();

//				ImageCropWithRubberBand mm = new ImageCropWithRubberBand();
//				Scene scene = mm.newSkill();
				Stage stage = new Stage();
				Text text = new Text("功能正在完善中");
				StackPane stack = new StackPane();
				stack.getChildren().add(text);

				Scene scene = new Scene(stack, 1024, 768);
				stage.setScene(scene);
				stage.show();
			}
		});


		//设置形状
		go.setStyle(
				"-fx-background-radius: 5em; " +
						"-fx-min-width: 22px; " +
						"-fx-min-height: 22px; " +
						"-fx-max-width: 22px; " +
						"-fx-max-height: 22px;"
		);
		textField.setStyle("-fx-background-radius: 2em; "+
								"-fx-min-height: 25px; " +
								"-fx-max-height: 25px;"
		);

		BorderPane containpane = new BorderPane();
		containpane.setLeft(mainMenuBar);
		containpane.setRight(stackpanetest);


		BorderMainpane.setTop(containpane);


		//获取home目录的路径
		String showpath = System.getProperty("user.home");
		TreeView<File> treeFile = new Filebrower().buildFileSystemBrowser(showpath);
		treeFile.setOnMouseClicked(t->{
		});

		treeFile.setMinWidth(200);
		Mainpane.getItems().add(treeFile);

		
		cmRight.getItems().addAll(openImage, copyImage, renameImage, Img_delete, Img_attriBute);
		Img_attriBute.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
		//集成目录树的右键菜单
		menuTree.getItems().addAll(cpAll, opAll, showAll, showSigle);
		menuTree.getItems().add(cpAll);

		//中间位置设置为Flowplane
		//+++++++++++++++++++++++++++++++++++++++++++
		BorderMainpane.setCenter(Mainpane);
		centerpane.setMinWidth(600);
		Mainpane.getItems().add(centerpane);

		Scene scene = new Scene(BorderMainpane,1300,803);
		Mainstage.setScene(scene);
		Mainstage.show();

		c = new Controller();
		listener = new MyLeapListener(this);
		c.addListener(listener);

		//设置分页栏的比例大小
		Mainpane.setDividerPositions(0.24f, 0.76f);
		Mainpane.setOnMouseMoved(e->{
//			System.out.println("鼠标移动");
			Mainpane.setOnMousePressed(ee->{
				System.out.println("鼠标移动");
				System.out.println("显示面板的宽度是："+centerpane.getWidth());
				Mainpane.setOnMouseReleased(r->{
					System.out.println(centerpane.getWidth());
				});
			});
		});
		Mainpane.setOnDragDetected(e->{
			System.out.println(centerpane.getWidth());
		});
		Mainpane.setOnDragDone(e->{
			System.out.println(centerpane.getWidth());
		});
		
		
	}
	Listener listener;
	Controller c;

	public void closeAll(){
		System.exit(0);
	}

    public void showhelp(){
        new Helpstage().startHelp();
    }
	

	public static void start(String[] args) {
		launch(args);

	}

	public static SimpleSlideShow simpleSlideShow = new SimpleSlideShow();
	public static Stage tmpstage = new Stage();

	public void setShowAll(){

        String AimnameforShow = pathtmp;
        simpleSlideShow.setAll(AimnameforShow);
        StackPane tmppane = simpleSlideShow.getRoot();
//        Stage tmpstage = new Stage();
        Scene tmpscene = new Scene(tmppane, 1000, 600);
        tmpstage.setScene(tmpscene);
        tmpstage.show();
        simpleSlideShow.start();
        if (pathtmp != null){
            System.out.println(pathtmp);
        }
//        centerpane.getChildren().clear();
//        System.exit(0);
	}

	public static void setShowAllclose(){
		tmpstage.setFullScreen(true);
//        count = 0;
	}

    public static void setset(){
        tmpstage.close();
    }

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		launch(args);

//        setShowAll();

		Scanner s = new Scanner(System.in);
		int a = s.nextInt();
		
		
		

	}

}
